package com.example.intermediateactivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.intermediateactivity.adapters.CustomBaseAdapter;
import com.example.intermediateactivity.beans.Dog;
import com.example.intermediateactivity.utils.ToastHelper;
import com.example.intermediateactivity.utils.Utils;

import java.util.List;

public class ListViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {

    private List<Dog> dataList;
    private CheckBox cbDivider;
    private CheckBox cbBackground;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        dataList = Dog.getDefaultList();

        listView = findViewById(R.id.lv_list);
        CustomBaseAdapter adapter = new CustomBaseAdapter(this, dataList);

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        cbBackground = findViewById(R.id.cb_background);
        cbBackground.setOnCheckedChangeListener(this);

        cbDivider = findViewById(R.id.cb_divider);
        cbDivider.setOnCheckedChangeListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ToastHelper.show(this, "点击了" + dataList.get(i).name);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        int id = compoundButton.getId();
        boolean isDividerCheck = R.id.cb_divider == id;
        boolean isBgCheck = R.id.cb_background == id;

        boolean isShowDivider = isDividerCheck && b;
        boolean isHideDivider = isDividerCheck && !b;

        boolean isShowBg = isBgCheck && b;
        boolean isHideBg = isBgCheck && !b;

        if (isShowDivider) {
            // 设置分割线颜色
            Drawable drawable = getResources().getDrawable(R.color.black, getTheme());
            listView.setDivider(drawable);
            // 设置分割线高度
            listView.setDividerHeight(Utils.dip2px(this, 1));
            return;
        }

        if (isHideDivider) {
            // 隐藏分割线
            listView.setDivider(null);
            // 分割线高度
            listView.setDividerHeight(Utils.dip2px(this, 1));
            return;
        }

        if (isShowBg) {
            listView.setSelector(R.drawable.list_selector);
        }

        if (isHideBg) {
            Drawable drawable = getResources().getDrawable(R.color.transparent, getTheme());
            listView.setSelector(drawable);
        }


    }
}